package com.gx.smart.construction.ui.common.viewmodel;

import androidx.fragment.app.FragmentActivity;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.construction.ui.common.model.AreaName;
import com.gx.smart.construction.ui.common.model.BuildingName;
import com.gx.smart.construction.ui.common.model.CommonItemLabel;
import com.gx.smart.construction.ui.common.model.ElevatorName;
import com.gx.smart.construction.ui.common.model.UnitName;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.callback.CircleItemLabel;

import java.util.List;

/**
 * @Author: xiao sy
 * 时间: 2022/2/10
 * @Description: 处理底部对话框
 * @Version: 1.0
 **/
public class BottomDialogUtil {

    /**
     * 请求的数据转化为目标数据
     * @param targetList
     * @param typeItem
     * @param tips
     * @param sourceList
     * @param <T>
     */
    public static  <T extends CommonItemLabel> void addConversionList(List<T> targetList,
                                                               T typeItem,
                                                               String tips,
                                                               List<ConstructionAppOrganizationalStructureDto> sourceList) {
        if (sourceList == null || sourceList.isEmpty()) {
            ToastUtils.showLong(tips);
            return;
        }
        targetList.clear();
        for (ConstructionAppOrganizationalStructureDto item : sourceList) {
            CommonItemLabel targetItem = null;
            if(typeItem instanceof AreaName){
                targetItem  = new AreaName();
            }
            if(typeItem instanceof BuildingName){
                targetItem  = new BuildingName();
            }
            if(typeItem instanceof UnitName){
                targetItem  = new UnitName();
            }

            targetItem.setContent(item.getName());
            targetItem.setId(item.getId());
            targetList.add((T)targetItem);
        }
    }

    public static <T extends CircleItemLabel> void showBottomDialog(FragmentActivity activity, List<T> list, SelectRoomViewModel viewModel) {
        new CircleDialog.Builder()
                .setItems(list, (parent, view, position, id) -> {
                    T t = list.get(position);
                    handleBottomDialog(t, viewModel);
                    return true;
                })
                .setNegative("取消", null)
                .show(activity.getSupportFragmentManager());
    }

    private static <T extends CircleItemLabel> void handleBottomDialog(T object, SelectRoomViewModel viewModel) {
        if (object instanceof AreaName) {
            AreaName item = (AreaName) object;
            viewModel.changeAreaName(item);
            viewModel.changeBuildingName(null);
            viewModel.changeUnitName(null);
        }
        if (object instanceof BuildingName) {
            BuildingName item = (BuildingName) object;
            viewModel.changeBuildingName(item);
            viewModel.changeUnitName(null);
        }

        if (object instanceof UnitName) {
            UnitName item = (UnitName) object;
            viewModel.changeUnitName(item);
        }

        viewModel.changeRoomName(null);
        viewModel.changeSn("");
    }
}
